//
//  HTZViewController.swift
//  PassValueDemo
//
//  Created by MAC on 2020/8/20.
//  Copyright © 2020 MAC. All rights reserved.
//

import UIKit

class HTZViewController: UIViewController {
    //声明全局变量名
    var textLabel_1 = UILabel()
    override func viewDidLoad() {
        super.viewDidLoad()
        //声明变量，并初始化
        let ymBtn_01 = UIButton()
        ymBtn_01.frame = CGRect(x: 50, y: 130, width: 220, height: 50)
        ymBtn_01.setTitle("跳转发送通知页面", for: .normal)
        ymBtn_01.backgroundColor = .brown
        ymBtn_01.tag = 10001
        ymBtn_01.addTarget(self, action: #selector(tiaozhuanyemianclick(sender:)), for: .touchUpInside)
        view.addSubview(ymBtn_01)
        
         //声明变量，并初始化
        let textLabel = UILabel()
        textLabel.font = .systemFont(ofSize: 20)
        textLabel.frame = CGRect(x: 50, y: ymBtn_01.frame.origin.y+150, width: 100, height: 50)
        textLabel.text = "接收传值："
        view.addSubview(textLabel)
        
         //接收传值的标签
        textLabel_1.backgroundColor = .darkGray
        textLabel_1.font = .systemFont(ofSize: 20)
        textLabel_1.frame = CGRect(x: (textLabel.frame.origin.x+textLabel.frame.size.width)+20, y: ymBtn_01.frame.origin.y+150, width: 200, height: 50)
        textLabel_1.text = "空值"
        textLabel_1.textColor = .white
        textLabel_1.numberOfLines = 0
        textLabel_1.lineBreakMode = .byWordWrapping
        view.addSubview(textLabel_1)
        
    }
    
    @objc func tiaozhuanyemianclick(sender:UIButton){
        
        let aTZVC = ATZViewController.init()
        aTZVC.title = "发送通知页面"
        aTZVC.view.backgroundColor = UIColor.init(named: "bgColor")
        
        /*
         创建通知中心
         设置监听方法
         设置通知的名字
         */
        NotificationCenter.default.addObserver(self, selector: #selector(login(_notification:)), name: NSNotification.Name(rawValue: "NotificationTest"), object: nil)
        //模态弹出
        self.present(aTZVC, animated: true, completion: nil)
    }
    
    //实现通知的监听方法，并在里面做好接收传值
    @objc private func login(_notification:Notification){
        
        let str = _notification.userInfo!["post"]
        textLabel_1.text = str as? String
        print("接收传值：",str!)
    }
}
